/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
function CheckBrowser(port)
            {
                /*if(navigator.userAgent.indexOf("Windows") > -1)
                {
                    window.location.assign("http://localhost:"+port+"/iOrderServer/login.jsp");
                }*/

                window.scrollTo(0,1);
            }
function selectlang(encookie,ok)
{
    if (navigator.cookieEnabled != true) {
        MSG1(encookie,ok);
        return;
    }
    var myurl = "waiter/selectlang.jsp";
	$.ajax({url:myurl,dataType:"html",
        success:function(result){
             $("#wrap").html(result);
        }
        });
    window.scrollTo(0,1);
}
function GopChuyen() {
    $.modal.close();
    var myurl = "waiter/waiter.jsp";
	$.ajax({url:myurl,dataType:"html",
        success:function(result){
            localStorage.curpos = "from";
            $('#joinsplit').modal(
            {
                focus:false,
                close:false,
                overlayClose:false
            }
            );
        }
        });
}
function CapNhatBan() {
    $.modal.close();
    var myurl = "waiter/waiter.jsp";
	$.ajax({url:myurl,dataType:"html",
        success:function(result){
            $('#updatetb').modal(
            {
                focus:false,
                close:false,
                overlayClose:false
            }
    );
        }
        });
}
function CapNhatTK() {
    $.modal.close();
    var myurl = "waiter/capnhattk.jsp";
	$.ajax({url:myurl,dataType:"html",
        success:function(result){
            $('#capnhattk').html(result);
            $('#capnhattk').modal(
            {
                focus:false,
                close:false,
                overlayClose:false
            }
    );
        }
        });
}
function LuuCapNhatTK(manv,ok) {
    
    var cptennv = $('#cptennv').val();
    var cpnewpass = $('#cpnewpass').val();
    var cpdiachi = $('#cpdiachi').val();
    var cpsodt = $('#cpsodt').val();
    var myurl = "NhanVien_Controller?action=CapNhatTKPV";
	$.ajax({url:myurl,dataType:"html",
        type:"POST",
        data:{tennv:cptennv,newpass:cpnewpass,diachi:cpdiachi,sodt:cpsodt,manv:manv},
        success:function(result){
            MSG1(result,ok);
            $.modal.close();
        }
        });
}
function Home()
{
    event.preventDefault();
    var myurl = "waiter/waiter.jsp";
	$.ajax({url:myurl,dataType:"html",
        success:function(result){
             $("#fclogin").html(result);
              {
                Show("fclogin");
            }
        }
        });
}
function Logout()
{
    var url = "DangNhap_Controller?action=clogout";
	$.ajax({url:url,dataType:"html",
        type:"POST",
        success:function(result){
                $("#cluser").css("background","#FFFFFF");
                $("#clpass").css("background","#FFFFFF");
                Show("fclogin");
        }
        });
    window.scrollTo(0,1);
}
function Login()
{
    var name = document.clogin.user.value;
    var pass = document.clogin.pass.value;
    if(name == "" || pass == "")
    {return;}
    var url = "DangNhap_Controller?action=clogin";
	$.ajax({url:url,dataType:"html",
        type:"POST",
        data:{user:name,pass:pass},
        success:function(result){
             if(result == "0")
             {
                $("#cluser").css("background","#EA0A0A");
                $("#clpass").css("background","#EA0A0A");
             }
             else {
                 localStorage.manv = result;
                 $("#cluser").css("background","#FFFFFF");
                $("#clpass").css("background","#FFFFFF");
                Show("fclogin");
             }
        }
        });
    window.scrollTo(0,1);
}
function SelectTable() {
    $.modal.close();
    var myurl = "waiter/waiter.jsp";
	$.ajax({url:myurl,dataType:"html",
        success:function(result){
            $('#tablemodal').modal(
            {
                focus:false,
                close:false,
                overlayClose:false
            }
    );
        }
        });
    
}
function NumTable(num) {
    var cur = $('#numtable').val();    
    if(num == "b")
    {
        if(cur.length > 0){
            cur = cur.substring(0, cur.length - 1);
        }
    }
    else {
        if(cur.length <5) {
            cur = cur + num;
        }        
    }   
    $('#numtable').val(cur);
}
function NumTable2(num) {
    var cur = $('#numupdate').val();
    if(num == "b")
    {
        if(cur.length > 0){
            cur = cur.substring(0, cur.length - 1);
        }
    }
    else {
        if(cur.length <5) {
            cur = cur + num;
        }
    }
    $('#numupdate').val(cur);
    $('#tbstate').val("");
    if(cur.length >0)
    {
        var url = "GhiPhieu_Controller?act=XemTTBan";
	$.ajax({url:url,dataType:"html",
        type:"POST",
        data:{ban:cur},
        success:function(result){
             $('#tbstate').val(result);
        }
        });
    }

}
function UpdateStateTB (tt,ok) {
    var ban = $('#numupdate').val();
    if(ban == "")
        return;
    var myurl = "GhiPhieu_Controller?act=CapNhatTTB";
                $.ajax(
                {url:myurl,
                    dataType:"html",
                    type:"POST",
                    data:{matt:tt,ban:ban},
                    success:function(result){
                      MSG1(result,ok);
                    }
                });
}
function NumTable1(num) {
    if (localStorage)
    { 
       var curpos = localStorage.curpos;
       var cur = $('#'+curpos).val();
    if(num == "b")
    {
        if(cur.length > 0){
            cur = cur.substring(0, cur.length - 1);
        }
    }
    else if(num == "m")
    {
        $('#exp').val(">>>");
            if(localStorage.curpos == "from")
            {
                localStorage.curpos = "to";
            }
            else {
                localStorage.curpos = "from";
            }

    }
    else if(num == "j")
    {
        $('#exp').val("+");
            if(localStorage.curpos == "from")
            {
                localStorage.curpos = "to";
            }
            else {
                localStorage.curpos = "from";
            }
    }
    else {
        if(cur.length <5) {
            cur = cur + num;
        }
    }
    $('#'+curpos).val(cur);
    }
}
function JoinMove(ok) {
    var exp = $('#exp').val();
    var from = $('#from').val();
    var to = $('#to').val();
    if(from != "" && to != "")
    {
        if(exp == "+")
        {
            var url = "DoiBan_Controller?act=gopBan";
	$.ajax({url:url,dataType:"html",
        type:"POST",
        data:{tuban:from,denban:to},
        success:function(result){
             MSG1(result,ok);
        }
        });
        }
        if(exp == ">>>")
        {
         var url1 = "DoiBan_Controller?act=chuyenBan";
	$.ajax({url:url1,dataType:"html",
        type:"POST",
        data:{tuban:from,denban:to},
        success:function(result){
             MSG1(result,ok);
        }
        });
        }
    }
}
function SetTableDV(suc,fal,ok) {
    var cur = $('#numtable').val();
    if (localStorage)
    {
        var myurl = "GhiPhieu_Controller?act=checkBan";
	$.ajax({url:myurl,dataType:"html",
        type:"POST",
        data:{ban:cur},
        success:function(result){
             if(result == "1")
             {
                 localStorage.tbnum= cur;
                 MSG1(suc,ok);
             }
             else
            {
                MSG1(fal,ok);
            }
        }
        });
        
    }
    else
    {
        MSG1(fal,ok);
    }
}


function main(langid)
{
     var url = "DangNhap_Controller?action=user&lang="+langid;
	$.ajax({url:url,dataType:"html",
        success:function(result){            
        }
        });
     var myurl = "waiter/mainclient.jsp";
	$.ajax({url:myurl,dataType:"html",
        success:function(result){
             $("#wrap").html(result);  
        },
        complete:function(){
            var url1 = "waiter/listfood.jsp";
             $.ajax(
                {url:url1,
                    dataType:"html",
                    type:"POST",
                    data:{loai:"1",nhom:"",tenma:""},
                    success:function(result){
                        $("#slidesContainer").html(result);
                    },
                    complete:function(){
                        var url1 = "waiter/groupfood.jsp";
                         $.ajax(
                            {url:url1,
                                dataType:"html",
                                type:"POST",
                                data:{loai:"1",nhom:"",tenma:"",start: "0"},
                                success:function(result){
                                    $("#pagefood0").html(result);
                                },
                                complete:function(){
                                    SlideForm('1',"","");
                                    $("#drinkt").css('color','#722105');
                                    $('#guide').modal(
                                    {
                                        focus:false,
                                        close:false,
                                        overlayClose:false
                                    }
                                    );
                                }
                         });
                    }
             });                     
        }
        });
    window.scrollTo(1,0);
}
function Guide() {
    $('#guide').modal(
                        {
                            focus:false,
                            close:false,
                            overlayClose:false
                        }
                     );
}
function listfood(loai,nhom,tenma)
{
    event.preventDefault();
    $("#slideshow").html("");
    $("#slideshow").html("<div id='slidesContainer'></div>");

     var url1 = "waiter/listfood.jsp";
             $.ajax(
                {url:url1,
                    dataType:"html",
                    type:"POST",
                    data:{loai:loai,nhom:nhom,tenma:tenma},
                    success:function(result){
                        $("#slidesContainer").html(result);
                    },
                    complete:function(){
                        var url = "waiter/groupfood.jsp";
                         $.ajax(
                            {url:url,
                                dataType:"html",
                                type:"POST",
                                data:{loai:loai,nhom:nhom,tenma:tenma,start: "0"},
                                success:function(result){
                                    $("#pagefood0").html(result);
                                },
                                complete:function(){
                                    SlideForm(loai,nhom,tenma);
                                    if(loai == '2')
                                    {
                                        $("#drinkt").css('color','#f7eead');
                                        $("#foodt").css('color','#722105');
                                    }
                                    else
                                    {
                                         $("#drinkt").css('color','#722105');
                                        $("#foodt").css('color','#f7eead');
                                    }
                                }
                         });
                    }
             });
    window.scrollTo(1,0);
}
function CSearch()
{
    var key = document.csearch.keyword.value;
    listfood("","",key);
}
function ghiphieu()
{
     var myurl = "lapphieu.jsp";
	$.ajax({url:myurl,dataType:"html", success:function(result){
            $("#wrap").html(result);
        }});
    window.scrollTo(1,0);
}
function capnhatphieu()
{
     var myurl = "capnhatphieu.jsp";
	$.ajax({url:myurl,dataType:"html", success:function(result){
            $("#wrap").html(result);
        }});
    window.scrollTo(1,0);
}
function capnhatban()
{
     var myurl = "capnhatban.jsp";
	$.ajax({url:myurl,dataType:"html", success:function(result){
            $("#wrap").html(result);
        }});
    window.scrollTo(1,0);
}
function gopban()
{
     var myurl = "gopban.jsp";
	$.ajax({url:myurl,dataType:"html", success:function(result){
            $("#wrap").html(result);
        }});
    window.scrollTo(1,0);
}
function chuyenban()
{
     var myurl = "chuyenban.jsp";
	$.ajax({url:myurl,dataType:"html", success:function(result){
            $("#wrap").html(result);
        }});
    window.scrollTo(1,0);
}
function demo()
{
     var myurl = "demo.jsp";
	$.ajax({url:myurl,dataType:"html", success:function(result){
            $("#wrap").html(result);
            SlideForm();
           
        }});
        //AjaxNoiDung("GET", "demo.jsp","wrap");
}

function ClearValue(id)
{
    $("#"+id).val("");
}
function LoadBan(tt,ma,ten,kv)
{
    var area = $("#"+kv).val();
    $.getJSON("../GhiPhieu_Controller?act=loadBan&kv="+area+"&tt="+tt, function (data)
    {
           var datacopy = [];
           for (var i = 0; i < data.length; i++)
           {
                datacopy.push({
                    id: data[i].id,
                    label : data[i].value,
                    value: data[i].value

                });
           }
           $("#"+ten).autocomplete({
            source: datacopy,
            autoFocus: true,
            minLength: 0,
            select: function (event, ui) {
               $("#"+ma).val(ui.item.id);
            }
        });
    });
}
function LoadKhuVuc(ma,ten)
{
    $.getJSON("../GhiPhieu_Controller?act=loadKV", function (data)
    {
           var datacopy = [];
           for (var i = 0; i < data.length; i++)
           {
                datacopy.push({
                    id: data[i].id,
                    label : data[i].value,
                    value: data[i].value
                });
           }
           $("#"+ten).autocomplete({
            source: datacopy,
            autoFocus: true,
            minLength: 0,
            select: function (event, ui) {
                $("#"+ma).val(ui.item.id);
            }
        });
    });
}
function LoadDVT()
{
    document.phieugoi.madvt.value = "";
    var ma = document.phieugoi.mama.value;
    $.getJSON("../GhiPhieu_Controller?act=loadDVT&ma="+ma, function (data)
    {
           var datacopy = [];
           for (var i = 0; i < data.length; i++)
           {
                datacopy.push({
                    id: data[i].id,
                    label : data[i].value,
                    value: data[i].value
                });
           }
           $("#dvt").autocomplete({
            source: datacopy,
            autoFocus: true,
            minLength: 0,
            delay: 0,
            select: function (event, ui) {
                document.phieugoi.madvt.value = ui.item.id;
            }
        });
    });
}
function LoadMonAn()
{
    document.phieugoi.mama.value ="";
    var ma = document.phieugoi.monan.value;
           $("#monan").autocomplete({
            source: "../GhiPhieu_Controller?act=loadMA&tenma="+ma,
            minLength: 2,
            select: function (event, ui) {
                document.phieugoi.mama.value = ui.item.id;
                document.phieugoi.dvt.value = "";
                document.phieugoi.madvt.value = "";
                LoadDVT();
                var myurl = "../GhiPhieu_Controller?act=loadTuyChon&ma="+ui.item.id;
                $.ajax({url:myurl,dataType:"html", success:function(result){
                     $("#tuychon").html(result);
                }});
            }
        });
}
function Zoom(ha,mota,skip)
{
    var nd = "<div> <img src='images/foodimages/"+ha+"'/> </div> <div><p>"+mota+ "</p><input type='button' value='"+skip+"' class='simplemodal-close'/> </div>";
    $("#zoom").html(nd);
    $('#zoom').modal(
                                    {
                                        focus:false,
                                        zIndex : 500,
                                        close:false,
                                        closeHTML:null,
                                        overlayClose:false
                                    }
                                    );
}
function GoiMon(selecttb,ok,mama,tenma,madvt,tendvt,hinhanh,gia)
{
    //LoadDSTC();
    //MSG("Thông báo/Thông tin không hợp lệ");
    var ban = localStorage.tbnum;
    if(ban == null || ban == "")
    {
         MSG1(selecttb,ok);
        return;
    }
    var myurl = "GhiPhieu_Controller?act=goiMon";
                $.ajax(
                {url:myurl,
                    dataType:"html",
                    type:"POST",
                    data:{tenma:tenma,mama: mama, gia: gia,tendvt:tendvt,madvt:madvt,hinhanh:hinhanh,ban:ban},
                    success:function(result){
                        if(result != "")
                        {
                            if(result == "0")
                            {
                                $("#"+mama+madvt+" .extra").remove();
                                $("#item"+mama+madvt).remove();
                            }
                            else {
                                var sl = " "+result;
                                $("#"+mama+madvt+" .extra").remove();
                                var nd = "<label onclick=\"BotMon('"+selecttb+"','"+ok+"','"+mama+"','"+madvt+"',0);\" class='extra'>"+sl+"</label><img onclick=\"GhiChu('"+selecttb+"','"+ok+"','"+mama+"','"+madvt+"',0);\" class='extra'  src='images/note.png'/>";
                                //var nd = "<input onclick=\"BotMon('"+mama+"','"+madvt+"');\" class='extra' value=' "+sl+"' type='text'/><img class='extra'  src='images/note.png'/>";
                                $("#"+mama+madvt).append(nd);
                                $("."+mama+madvt).val(result);
                            }
                        }
                    }
                });
}
function BotMon(selecttb,ok,mama,madvt,reload)
{
    //LoadDSTC();
    var ban = localStorage.tbnum;
    if(ban == null || ban == "")
    {
         MSG1(selecttb,ok);
        return;
    }
    var myurl = "GhiPhieu_Controller?act=botMon";
                $.ajax(
                {url:myurl,
                    dataType:"html",
                    type:"POST",
                    data:{mama: mama,madvt:madvt,ban:ban},
                    success:function(result){
                 
                        if(result == "0")
                        {
                            $("#"+mama+madvt+" .extra").remove();
                            $("#item"+mama+madvt).remove();
                        }
                        else if(result == "-1")
                        {
                            $("#"+mama+madvt+" .extra").remove();
                            $("#item"+mama+madvt).remove();
                            if(reload == 1)
                            XemPhieu(selecttb,ok);
                        }
                        else {
                            var sl = " "+result;
                            $("#"+mama+madvt+" .extra").remove();
                            var nd = "<label onclick=\"BotMon('"+selecttb+"','"+ok+"','"+mama+"','"+madvt+"',0);\" class='extra'>"+sl+"</label><img onclick=\"GhiChu('"+selecttb+"','"+ok+"','"+mama+"','"+madvt+"',0);\" class='extra'  src='images/note.png'/>";
                             $("#"+mama+madvt).append(nd);
                             $("."+mama+madvt).val(result);
                        }
                        
                    }
                });
}
function XemCart(selecttb,ok,ban) {
    event.preventDefault();
    if(ban == null || ban == "")
    {
        MSG1(selecttb,ok);
        return;
    }
     var myurl = "waiter/cartmain.jsp";
                $.ajax(
                {url:myurl,
                    dataType:"html",
                    type:"POST",
                    data:{ban:ban},
                    success:function(result){
                        $("#chitietphieu").html(result);
                    },
                    complete:function() {
                       $("#tab ul li.right").css('background','-webkit-gradient(linear, left top, left bottom, color-stop(0%,rgba(255,251,126,1)), color-stop(99%,rgba(255,156,0,1)))');
                       $("#tab ul li.left").css('background','transparent');
                    }
                });

}
function loaded() {
                myScroll = new iScroll('chitietphieu');
            }
function XemPhieu(selecttb,ok)
{
    event.preventDefault();
    var ban = localStorage.tbnum;
    if(ban == null || ban == "")
    {
        MSG1(selecttb,ok);
        return;
    }
     var myurl = "waiter/cart.jsp";
                $.ajax(
                {url:myurl,
                    dataType:"html",
                    type:"POST",
                    data:{ban:ban},
                    success:function(result){
                        $("#phieu").html(result);
                    },
                    complete:function() {
                        $('#phieu').modal(
                                    {
                                        focus:false,
                                        zIndex : 500,
                                        close:false,
                                        closeHTML:null,
                                        overlayClose:false
                                    }
                                    );
                       $("#tab ul li.left").css('background','-webkit-gradient(linear, left top, left bottom, color-stop(0%,rgba(255,251,126,1)), color-stop(99%,rgba(255,156,0,1)))');
                       $("#tab ul li.right").css('background','transparent');
                    }
                });
}
function GhiChu(selecttb,ok,mama,madvt,showphieu)
{
    $.modal.close();
    var ban = localStorage.tbnum;
    if(ban == null || ban == "")
    {
        MSG1(selecttb,ok);
        return;
    }
     var myurl = "waiter/tuychon.jsp";
                $.ajax(
                {url:myurl,
                    dataType:"html",
                    type:"POST",
                    data:{ban:ban,mama:mama,madvt:madvt,showphieu:showphieu},
                    success:function(result){
                        $("#ghichu").html(result);
                    },
                    complete:function() {
                        $('#ghichu').modal(
                                    {
                                        focus:false,
                                        close:false,
                                        closeHTML:null,
                                        overlayClose:false
                                    }
                                    );
                                        /*
                        $('#ghichu').css('display','block');
                        $('#ghichu').css('z-index','900');
                        $('#ghichu').css('position','fixed');
                        $('#ghichu').css({'top':'10%','left':'5%'});*/
                    }
                });
}

function LuuTuyChon(selecttb,ok,rqsent,mama,madvt,showphieu)
{
    var dstc = "";
    var tuychon = "";
    var ban = localStorage.tbnum;
    if(ban == null || ban == "")
    {
        MSG1(selecttb,ok);
        return;
    }
    if(document.phieugoi.dstc != null)
    {
        for(var i=0;i< document.phieugoi.dstc.length;i++)
        {
            if(document.phieugoi.dstc[i].checked == true)
                {
                    dstc = dstc + document.phieugoi.dstc[i].value+ ",";
                }
        }
        dstc = dstc.substring(0, dstc.length - 1);
    }

   var ghichu = document.phieugoi.ghichu.value;
   ghichu = ghichu.replace(/&&/gi,",");
       if(ghichu.length >0)
            tuychon = dstc+"&&"+ghichu;
        else
            tuychon = dstc+"&&    ";
    var myurl = "GhiPhieu_Controller?act=ghichu";
                $.ajax(
                {url:myurl,
                    dataType:"html",
                    type:"POST",
                    data:{ban:ban,mama: mama,madvt:madvt, note: tuychon},
                    success:function(result){
                        if(result =="1")
                        {
                            MSG1(rqsent,ok);
                           
                        }
                    }
                });
    $.modal.close();
    if(showphieu == 1)
     {
        XemPhieu(selecttb,ok);
     }
}
function DongTuyChon(selecttb,ok,showphieu)
{
    $.modal.close();
    if(showphieu == 1)
     {
        XemPhieu(selecttb,ok);
     }
}
function HoanTat(selecttb,ok,invalid)
{
    
     var ban = localStorage.tbnum;
     var manv = localStorage.manv;
     if(ban == null || ban == "")
    {
         MSG1(selecttb,ok);
        return;
    }
    if(ban == "" || manv =="" )
    {
        MSG1(invalid,ok);
        return;
    }
    
    var myurl = "GhiPhieu_Controller?act=lapPhieu";
                $.ajax(
                {url:myurl,
                    dataType:"html",
                    type:"POST",
                    data:{ban:ban,nv:manv},
                    success:function(result){
                       MSG1(result,ok);
                       $.modal.close();
                       $(".extra").remove();
                    }
                });
}
function HuyPhieu(ok,cancel,ban,sure)
{
    $.alerts.okButton = ok;
            $.alerts.cancelButton = cancel;
            jConfirm(sure, 'i-Order System', function(r) {
                 window.scrollTo(0,1);
                if(r)
                 {
                     var myurl = "GhiPhieu_Controller?act=huyPhieu";
                    $.ajax(
                    {url:myurl,
                        dataType:"html",
                        type:"POST",
                        data:{ban:ban},
                        success:function(result){
                            $.modal.close();
                            $(".extra").remove();
                        }
                    });
                 }
            });
}
function ShowGhiChu()
{
            $(".ghichu").slideToggle(400);
            $(".phieuchinh").slideToggle(400);
}
function BoMA(ma,ban)
{
   var myurl = "../GhiPhieu_Controller?act=boMA";
                $.ajax(
                {url:myurl,
                    dataType:"html",
                    type:"POST",
                    data:{ma:ma,ban:ban},
                    success:function(result){
                       $("#phieu").html(result);
                    }
                });
}
// Update Table
function LoadBanCNB(tt)
{
    document.capnhatban.maban.value= "";
    var area = document.capnhatban.makv.value;
    $.getJSON("../GhiPhieu_Controller?act=loadBan&kv="+area+"&tt="+tt, function (data)
    {
           var datacopy = [];
           for (var i = 0; i < data.length; i++)
           {
                datacopy.push({
                    id: data[i].id,
                    label : data[i].value,
                    value: data[i].value

                });
           }
           $("#bancnb").autocomplete({
            source: datacopy,
            autoFocus: true,
            minLength: 1,
            delay: 0,
            select: function (event, ui) {
                var ban = ui.item.id;
                document.capnhatban.maban.value = ban;
                var myurl = "../GhiPhieu_Controller?act=XemTTBan";
                $.ajax(
                {url:myurl,
                    dataType:"html",
                    type:"POST",
                    data:{ban:ban},
                    success:function(result){
                       document.capnhatban.curstate.value= result;
                    }
                });
            }
        });
    });
}
function LoadKhuVucCNB()
{
    document.capnhatban.makv.value = "";
    $.getJSON("../GhiPhieu_Controller?act=loadKV", function (data)
    {
           var datacopy = [];
           for (var i = 0; i < data.length; i++)
           {
                datacopy.push({
                    id: data[i].id,
                    label : data[i].value,
                    value: data[i].value
                });
           }
           $("#khuvuccnb").autocomplete({
            source: datacopy,
            autoFocus: true,
            minLength: 1,
            delay: 0,
            select: function (event, ui) {
                document.capnhatban.makv.value = ui.item.id;
                LoadBanCNB('change');
            }
        });
    });
}
function LoadDSTTBan()
{
    document.capnhatban.mattban.value = "";
    $.getJSON("../GhiPhieu_Controller?act=LoadDSTTBan", function (data)
    {
           var datacopy = [];
           for (var i = 0; i < data.length; i++)
           {
                datacopy.push({
                    id: data[i].id,
                    label : data[i].value,
                    value: data[i].value
                });
           }
           $("#ttban").autocomplete({
            source: datacopy,
            autoFocus: true,
            minLength: 1,
            delay: 0,
            select: function (event, ui) {
                document.capnhatban.mattban.value = ui.item.id;
            }
        });
    });
}
function HoanTatCNTTB()
{
    var ttmoi =  document.capnhatban.mattban.value ;
    var ban = document.capnhatban.maban.value;
    var myurl = "../GhiPhieu_Controller?act=CapNhatTTB";
                $.ajax(
                {url:myurl,
                    dataType:"html",
                    type:"POST",
                    data:{matt:ttmoi,ban:ban},
                    success:function(result){
                      alert(result);
                    }
                });
   
}
function HoanTatGopBan(sure)
{
    var kq = confirm(sure);
    if(kq)
    {
        var tuban = $("#gb_matuban").val() ;
        var denban = $("#gb_madenban").val() ;
        var myurl = "../DoiBan_Controller?act=gopBan";
                    $.ajax(
                    {url:myurl,
                        dataType:"html",
                        type:"POST",
                        data:{tuban:tuban,denban:denban},
                        success:function(result){
                          alert(result);
                        }
                    });
    }
}
function HoanTatChuyenBan(sure)
{
    var kq = confirm(sure);
    if(kq)
    {
        var tuban = $("#cb_matuban").val() ;
        var denban = $("#cb_madenban").val() ;
        var denkv = $("#cb_madenkv").val() ;
        var myurl = "../DoiBan_Controller?act=chuyenBan";
                    $.ajax(
                    {url:myurl,
                        dataType:"html",
                        type:"POST",
                        data:{tuban:tuban,denban:denban,denkv:denkv},
                        success:function(result){
                          alert(result);
                        }
                    });
    }
}
function TinhTien(ok,cancel,sure)
{
    var ban = localStorage.tbnum;
    $.alerts.okButton = ok;
    $.alerts.cancelButton = cancel;
    jConfirm(sure, 'i-Order System', function(r) {
         window.scrollTo(0,1);
        if(r)
        {
            var myurl = "DoiBan_Controller?act=tinhtien";
                    $.ajax(
                    {url:myurl,
                        dataType:"html",
                        type:"POST",
                        data:{ban:ban},
                        success:function(result){
                          MSG1(result,ok);
                        }
                    });
        }
    });
}
function LoadBanCNP(tt,ma,ten,kv)
{
    var area = $("#"+kv).val();
    $.getJSON("../GhiPhieu_Controller?act=loadBan&kv="+area+"&tt="+tt, function (data)
    {
           var datacopy = [];
           for (var i = 0; i < data.length; i++)
           {
                datacopy.push({
                    id: data[i].id,
                    label : data[i].value,
                    value: data[i].value

                });
           }
           $("#"+ten).autocomplete({
            source: datacopy,
            autoFocus: true,
            minLength: 0,
            select: function (event, ui) {
                $("#"+ma).val(ui.item.id);
               var myurl = "../DoiBan_Controller?act=loadcart";
               var ban = ui.item.id;
                    $.ajax(
                    {url:myurl,
                        dataType:"html",
                        type:"POST",
                        data:{ban:ban,item:1},
                        success:function(result){
                          $("#cnphieu").html(result);
                        }
                    });
            }
        });
    });
}
function Refresh(item)
{
    var ban = $("#cnp_maban").val();
    if(ban != "")
    {
        var myurl = "../DoiBan_Controller?act=loadcart";
                    $.ajax(
                    {url:myurl,
                        dataType:"html",
                        type:"POST",
                        data:{ban:ban,item:item},
                        success:function(result){
                          $("#cnphieu").html(result);
                        }
                    });
    }
}
function HoiThuc(macthd,ok)
{
    event.preventDefault();
        var myurl = "DoiBan_Controller?act=hoithuc";
                    $.ajax(
                    {url:myurl,
                        dataType:"html",
                        type:"POST",
                        data:{ma:macthd},
                        success:function(result){
                          MSG1(result,ok);
                        }
                    });
}
function XoaMon(selecttb,ok,ma)
{
    var ban = localStorage.tbnum;
    if(ban == null || ban == "")
    {
        MSG1(selecttb,ok);
        return;
    }
   var myurl = "DoiBan_Controller?act=xoaMA";
                $.ajax(
                {url:myurl,
                    dataType:"html",
                    type:"POST",
                    data:{ma:ma},
                    success:function(result){
                       if(result == "1")
                       {
                           XemCart(selecttb,ok,ban);
                       }
                       else
                           {
                               MSG1(result,ok);
                               XemCart(selecttb,ok,ban);
                           }
                    }
                });
}
 function SlideForm(loai,nhom,tenma){
  var currentPosition = 0;
  var slideWidth = 320;
  var slides = $('.slide');
  var numberOfSlides = slides.length;
  // Remove scrollbar in JS
  $('#slidesContainer').css('overflow', 'hidden');

  // Wrap all .slides with #slideInner div
  slides
    .wrapAll('<div id="slideInner"></div>')
    // Float left to display horizontally, readjust .slides width
	.css({
      'float' : 'left',
      'width' : slideWidth
    });

  // Set #slideInner width equal to total width of all slides
  $('#slideInner').css('width', slideWidth * numberOfSlides);

  // Insert controls in the DOM
  /*$('#slideshow')
    .prepend('<span class="control" id="leftControl">Clicking moves left</span>')
    .append('<span class="control" id="rightControl">Clicking moves right</span>');*/

  // Hide left arrow control on first load
  //manageControls(currentPosition);

  // Create event listeners for .controls clicks
  $('#slidesContainer').addSwipeEvents(function(evt, touch) {
       var a  = -1;
    if(touch.eventType =='swipeleft') {
         a  = currentPosition+1;
    }
    else if(touch.eventType =='swiperight')
    {
            a= currentPosition-1;
    }
    else {
        
    }
        if(a <0 || a > numberOfSlides-1)
        {return;}
        currentPosition = a;
        if($("#pagefood"+currentPosition).html() == "")
        {
            var url1 = "waiter/groupfood.jsp";
            var start =currentPosition*5;
             $.ajax(
                {url:url1,
                    dataType:"html",
                    type:"POST",
                    data:{loai:loai,nhom:nhom,tenma:tenma,start: start},
                    success:function(result){
                        $("#pagefood"+currentPosition).html(result);
                    },
                    complete:function(){
                         $('#slideInner').animate({
                          'marginLeft' : slideWidth*(-currentPosition)
                        },500);
                    }
             });
        }
        else
        {
            $('#slideInner').animate({
                          'marginLeft' : slideWidth*(-currentPosition)
                        },500);
        }
    }) ;

  function manageControls(position){
    // Hide left arrow if position is first slide
	if(position==0){$('#leftControl').hide()} else{$('#leftControl').show()}
	// Hide right arrow if position is last slide
    if(position==numberOfSlides-1){$('#rightControl').hide()} else{$('#rightControl').show()}
  }
}
// manageControls: Hides and Shows controls depending on currentPosition
   function Show(id){
   event.preventDefault();
   $("#"+id).slideToggle("600");
   window.scrollTo(1,0);
   return false;
  }
  function Modal(){
  
jQuery(function ($) {
	var OSX = {
		container: null,
		init: function () {
			$("input.osx, a.osx").click(function (e) {
				e.preventDefault();

				$("#osx-modal-content").modal({
					overlayId: 'osx-overlay',
					containerId: 'osx-container',
					closeHTML: null,
					minHeight: 80,
					opacity: 65,
					position: ['0',],
					overlayClose: true,
					onOpen: OSX.open,
					onClose: OSX.close
				});
			});
		},
		open: function (d) {
			var self = this;
			self.container = d.container[0];
			d.overlay.fadeIn('slow', function () {
				$("#osx-modal-content", self.container).show();
				var title = $("#osx-modal-title", self.container);
				title.show();
				d.container.slideDown('slow', function () {
					setTimeout(function () {
						var h = $("#osx-modal-data", self.container).height()
							+ title.height()
							+ 20; // padding
						d.container.animate(
							{height: h},
							200,
							function () {
								$("div.close", self.container).show();
								$("#osx-modal-data", self.container).show();
							}
						);
					}, 300);
				});
			})
		},
		close: function (d) {
			var self = this; // this = SimpleModal object
			d.container.animate(
				{top:"-" + (d.container.height() + 20)},
				900,
				function () {
					self.close(); // or $.modal.close();
				}
			);
		}
	};

	OSX.init();

});
  }